﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Hedgehog
{
    public partial class Form1 : Form
    {
        List<IFigure> figuresList = new List<IFigure>();
        List<IFigure> figures = new List<IFigure>();
        IFigure selectedFigure;
        int x;
        int y;
        private int clickX;
        private int clickY;

        public Form1()
        {

            InitializeComponent();
            figuresList.Add(new Circle(50, 50, 30));
            figuresList.Add(new BezierCurve(10,250,40,280,80,230));
            figuresList.Add(new Bristle(10, 470, 40, 450, 70, 470));
            figuresList.Add(new Ellipse(45, 350, 25, 40));
            figuresList.Add(new SerpinskiTriangle(new Point(15, 190), new Point(80, 190), new Point(50, 125), 3));

            //figures.Add(new Circle(300, 220, 15));
            //figures.Add(new Circle(305, 220, 5));
            //figures.Add(new Circle(400, 220, 15));
            //figures.Add(new Circle(395, 220, 5));
            //figures.Add(new BezierCurve(310, 350, 340, 390, 400, 350));
            //figures.Add(new BezierCurve(310, 350, 340, 300, 400, 350));
            //figures.Add(new BezierCurve(310, 350, 340, 380, 400, 350));
            //figures.Add(new BezierCurve(310, 350, 340, 310, 400, 350));
            //figures.Add(new BezierCurve(340, 220, 300, 310, 340, 300)); //nose
            //figures.Add(new Ellipse(100, 150, 350,250));
            //figures.Add(new Bristle(260, 150, 350, 70, 430, 150));
            //figures.Add(new SerpinskiTriangle(new Point(100,400), new Point(400,400),new Point(250,150),5 ));
        }



        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {

            
            Graphics g = e.Graphics;
            
            g.DrawString(clickX+" "+clickY,new Font("arial",14),Brushes.Red,750,450 );
            g.DrawLine(Pens.Black, 100,0,100,800);
            g.DrawLine(Pens.Black, 0, 100, 100, 100);
            g.DrawLine(Pens.Black, 0, 200, 100, 200);
            g.DrawLine(Pens.Black, 0, 300, 100, 300);
            g.DrawLine(Pens.Black, 0, 400, 100, 400);
            
            foreach (IFigure fig in figuresList)
            fig.Draw(g);

            foreach (IFigure f in figures)
                f.Draw(g);

        }

        #region bull shit
        private void Form1_Resize(object sender, EventArgs e)
        {
            pictureBox1.Width = Width;
            pictureBox1.Height = Height;

            pictureBox1.Invalidate();
        }
        #endregion

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            clickX = e.X;
            clickY = e.Y;
            pictureBox1.Invalidate();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (selectedFigure == null) return;
            selectedFigure.offsetX += e.X-x;
            selectedFigure.offsetY += e.Y-y;          
            pictureBox1.Invalidate();
            x = e.X;
            y = e.Y;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            selectedFigure = null;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            foreach (IFigure fig in figuresList)
            if (fig.ChekIfPointInside(e.X,e.Y))
            {
                x = e.X;
                y = e.Y;
                figures.Add((IFigure)fig.Clone());
                
                break;
            }
            
            foreach (IFigure f in figures)
                if (f.ChekIfPointInside(e.X, e.Y))
                {
                    //MessageBox.Show(f.GetType().ToString());
                    x = e.X;
                    y = e.Y;
                    selectedFigure = f;
                    break;
                }

        }
    }
}
